
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Pawn.h"
#include "PaperFlipbookComponent.h"
#include "Components/SceneComponent.h"
#include "PaperSpriteComponent.h"

#include "EnhancedInputComponent.h"
#include "EnhancedInputSubsystems.h"
#include "InputActionValue.h"
// #include "GameFramework/Controller.h"

#include "Character2D.generated.h"

UCLASS()
class XCZ2_API ACharacter2D : public APawn
{
	GENERATED_BODY()

protected:
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
	class UCapsuleComponent* CapsuleComponent;

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
	UPaperFlipbookComponent* CharacterFlipbook;

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
	class USpringArmComponent* SpringArm;

	UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
	class UCameraComponent* Camera;

	// 枪所需的组件
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
	USceneComponent* GunParent;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
	UPaperSpriteComponent* GunSprite;
	UPROPERTY(VisibleAnywhere, BlueprintReadOnly)
	USceneComponent* BulletSpawnPosition;
	
	// UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
	// class UCharacterMovementComponent* CharacterMovementComponent;

	// 角色可移动边界
	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	FVector2D HorizontalLimits;
	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	FVector2D VerticalLimits;

	// 子弹
	UPROPERTY(EditDefaultsOnly)
	TSubclassOf<class ABullet> BulletActorToSpawn;

	// 角色动画
	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	UPaperFlipbook* WalkFlipbook;
	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	UPaperFlipbook* IdleFlipbook;

	// 角色输入动作
	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	UInputMappingContext* InputMappingContext;
	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	UInputAction* MoveAction;
	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	UInputAction* ShootAction;

	// 角色移动属性
	UPROPERTY(EditAnywhere, BlueprintReadOnly)
	float MovementSpeed = 100.0f;
	UPROPERTY(BlueprintReadWrite)
	FVector2D MovementDirection;
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
	bool CanMove = true;

	// 射击属性
	UPROPERTY(VisibleAnywhere, BlueprintReadWrite)
	bool CanShoot = true;
	UPROPERTY(EditAnywhere, BlueprintReadWrite)
	float ShootCooldown = 0.3f;

	FTimerHandle ShootCooldownTimer;
	
private:
	bool IsInMapBoundsHorizontal(float XPos);
	bool IsInMapBoundsVertical(float YPos);
	
public:
	ACharacter2D();
	virtual void BeginPlay() override;
	virtual void Tick(float DeltaTime) override;
	virtual void SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) override;

	void MoveTriggered(const FInputActionValue& Value);
	void MoveCompleted(const FInputActionValue& Value);

	void Shoot(const FInputActionValue& Value);

	void OnShootCooldownTimerTimeout();
	
};
